﻿namespace IntegrationTests.ValueConversionTests
{
    using System;
    using System.Linq;
    using DirectoryServicesProtocolsExtensions.LdapConnection;
    using DirectoryServicesProtocolsExtensions.SearchRequest;

    using Microsoft.VisualStudio.TestTools.UnitTesting;

    [TestClass]
    public class GUIDConversionTests : SemanticValueConversionTest
    {
        private const string DNToTestAgainst = "CN=Administrator,CN=Users,DC=fabrikam,DC=com";

        [TestMethod]
        public void ObjectGUIDConversionTest()
        {
            // Get ObjectGUID via PowerShell
            const string attributeToTestAgainst = "objectGUID";
            const string scriptText =
                "Get-ADObject -identity " +
                "'" +
                DNToTestAgainst +
                "'" + " -properties " + 
                attributeToTestAgainst + 
                " | select -expandProperty " + 
                attributeToTestAgainst;
            var guidAquiredByPowerShell = (Guid)ExecutePowerShellScript(scriptText)[0].BaseObject;
            Assert.IsNotNull(guidAquiredByPowerShell);

            // Get ObjectSID via code under test
            var metaMapping = LdapConnection.GetAttributeMetaData(attributeToTestAgainst, LdapMetaMapFactory);
            var ldapQueryPropertiesContainer = new MockFabrikamActiveDirectoryQueryPropertiesContainer
            {
                SearchBaseDN = DNToTestAgainst,
                AttributesToLoad = new[] { attributeToTestAgainst }
            };

            var pagedSearchRequest = new PagedSearchRequest(ldapQueryPropertiesContainer);
            var codeResult = pagedSearchRequest.Execute(LdapConnection).Single()[0];
            var guidAquiredByCode = codeResult.Attributes[attributeToTestAgainst].GetValues(metaMapping.RawDotNetTypeEquivalent)[0];
            var guidSemanticallyConvertedByCode = metaMapping.SemanticValueConversionFunction(guidAquiredByCode);

            Assert.AreEqual(guidAquiredByPowerShell, guidSemanticallyConvertedByCode);
        }
    }
}
